﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Project
{
    public class Landmine : Button
    {
        #region 变量

        private GridStatus gridStatus = GridStatus.Close;
        private Style gridStyle = Style.Empty;
        private Point position;
        private int gridNo;
        private string numberValue;

        /// <summary>
        /// 方格状态
        /// </summary>
        public GridStatus GridStatus
        {
            get { return gridStatus; }
            set
            {
                switch (value)
                {
                    case GridStatus.Close:
                        break;
                    case GridStatus.Open:
                        switch (gridStyle)
                        {
                            case Style.Empty:
                                this.BackColor = Color.LightSlateGray;
                                this.FlatAppearance.BorderColor = Color.LightSlateGray;
                                this.Enabled = false;
                                break;
                            case Style.Landmine:
                                this.BackColor = Color.Red;
                                this.FlatAppearance.BorderColor = Color.Red;
                                this.Text = "*";
                                this.Enabled = false;
                                break;
                            case Style.Number:
                                this.BackColor = Color.LightGray;
                                this.FlatAppearance.BorderColor = Color.LightGray;
                                this.Text = this.numberValue;
                                this.Enabled = false;
                                break;
                            case Style.Flag:
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
                }
                gridStatus = value;
            }
        }

        /// <summary>
        /// 方格样式.
        /// </summary>
        public Style GridStyle
        {
            get { return gridStyle; }
            set
            {
                switch (value)
                {
                    case Style.Empty:
                        this.Text = string.Empty;
                        break;
                    case Style.Landmine:
                        break;
                    case Style.Number:
                        if (string.IsNullOrEmpty(this.numberValue))
                        {
                            this.numberValue = "1";
                        }
                        else
                        {
                            this.numberValue = (int.Parse(this.numberValue) + 1).ToString();
                        }
                        break;
                    case Style.Flag:
                        if (string.IsNullOrEmpty(this.Text))
                        {
                            this.Text = "◆";
                        }
                        else
                        {
                            this.Text = "";
                        }
                        break;
                    default:
                        break;
                }
                gridStyle = value;
            }
        }

        /// <summary>
        /// 方格位置
        /// </summary>
        public Point Position
        {
            get { return position; }
            set { position = value; }
        }

        /// <summary>
        /// 方格编号
        /// </summary>
        public int GridNo
        {
            get { return gridNo; }
            set { gridNo = value; }
        }

        /// <summary>
        /// 数字值
        /// </summary>
        public string NumberValue
        {
            get { return numberValue; }
            set { numberValue = value; }
        }

        #endregion

        public Landmine()
        {
            SetStyle(
               ControlStyles.UserPaint |
               ControlStyles.AllPaintingInWmPaint |
               ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.ResizeRedraw |
               ControlStyles.SupportsTransparentBackColor, true);
            this.TabStop = false;
            this.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

            this.Size = new Size(20, 20);
            this.Click += new EventHandler(Landmine_Click);
            this.MouseDown += new MouseEventHandler(Landmine_MouseDown);
        }

        void Landmine_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.GridStyle = Style.Flag;
            }
        }

        void Landmine_Click(object sender, EventArgs e)
        {
            if (this.GridStatus.Equals(GridStatus.Close))
            {
                switch (gridStyle)
                {
                    case Style.Empty:
                        break;
                    case Style.Landmine:
                        this.Text = "*";
                        break;
                    case Style.Number:
                        this.Text = this.numberValue;
                        break;
                    case Style.Flag:
                        return;
                    default:
                        break;
                }

                this.GridStatus = GridStatus.Open;
                this.Enabled = false;
            }
        }

        #region 重写

        /// <summary>
        /// 屏蔽button焦点框
        /// </summary>
        protected override bool ShowFocusCues
        {
            get
            {
                return false;
            }
        }

        #endregion
    }


    /// <summary>
    /// 方格样式
    /// </summary>
    public enum Style
    {
        /// <summary>
        /// 空
        /// </summary>
        Empty = 0,

        /// <summary>
        /// 地雷
        /// </summary>
        Landmine = 1,

        /// <summary>
        /// 数字
        /// </summary>
        Number = 2,

        /// <summary>
        /// 旗
        /// </summary>
        Flag = 3,
    }

    /// <summary>
    /// 方格状态
    /// </summary>
    public enum GridStatus
    {
        /// <summary>
        /// 关闭状态
        /// </summary>
        Close = 0,

        /// <summary>
        /// 打开状态
        /// </summary>
        Open = 1
    }

}
